package com.loongtech.bi.action.system;

import com.loongtech.bi.entity.system.EntitySysFunction;
import com.loongtech.bi.entity.system.EntitySysModel;
import com.loongtech.bi.entity.system.EntitySysRoleFunction;
import com.loongtech.bi.manager.system.SysFunctionManager;
import com.loongtech.bi.manager.system.SysLogManager;
import com.loongtech.bi.manager.system.SysModelManager;
import com.loongtech.bi.manager.system.SysRoleFunctionManager;
import com.loongtech.bi.support.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能管理
 *
 * @author xckuang
 * @date 2014-10-07
 */
@Controller("functionManagerAction")
@Scope("view")
public class FunctionManagerAction implements Serializable {
	private static final long serialVersionUID = 1L;
	@Autowired
	private Session session;
	@Autowired
	private SysLogManager sysLogManager;
	@Autowired
	private SysModelManager sysModelManager;
	@Autowired
	private SysFunctionManager sysFunctionManager;
	@Autowired
	private SysRoleFunctionManager sysRoleFunctionManager;
	private EntitySysFunction function;
	private Map<Integer, EntitySysModel> modelMap;
	private Map<Integer, String> statusMap;
	private List<EntitySysFunction> functionList = new ArrayList<>();
	private static final String MsgTag = "功能管理";

	@Autowired
	public void init() {
		setFunction(new EntitySysFunction());
		modelMap = new HashMap<>();
		for (EntitySysModel domain : sysModelManager.findAll()) {
			modelMap.put(domain.getId(), domain);
		}
		statusMap = sysFunctionManager.getStatusMap();
	}

	public String getMName(int modelId) {
		EntitySysModel model = modelMap.get(modelId);
		return null != model ? model.getDescription() : "根目录";
	}

	private boolean empty(String param) {
		return null == param || param.trim().isEmpty();
	}

	public void search() {
		List<Object> params = new ArrayList<Object>();
		if (function.getId() != null && function.getId() > 0) {
			params.add(EntitySysFunction.K.id);
			params.add(function.getId());
		}
		if (!empty(function.getName())) {
			params.add(EntitySysFunction.K.name);
			params.add(function.getName());
		}
		if (function.getModelId() > 0) {
			params.add(EntitySysFunction.K.modelId);
			params.add(function.getModelId());
		}
		if (!empty(function.getSrc())) {
			params.add(EntitySysFunction.K.src);
			params.add(function.getSrc());
		}
		if (function.getSort() > 0) {
			params.add(EntitySysFunction.K.sort);
			params.add(function.getSort());
		}
		if (function.getStatus() > 0) {
			params.add(EntitySysFunction.K.status);
			params.add(function.getStatus());
		}
		functionList.clear();
		if (params.isEmpty()) {
			functionList.addAll(sysFunctionManager.findAll());
		} else {
			functionList.addAll(sysFunctionManager.findBy(params.toArray(new Object[params.size()])));
		}
		sysLogManager.addLog(session.getUserName(), "功能查询成功!");
	}

	public void modify(EntitySysFunction function) {
		FacesContext context = FacesContext.getCurrentInstance();
		if (null == sysModelManager.get(function.getModelId())) {
			context.addMessage(MsgTag, new FacesMessage("功能修失败", "不存在模块modelId=" + function.getModelId()));
			return;
		}
		EntitySysFunction cur = sysFunctionManager.get(function.getId());
		if (cur == null) {
			context.addMessage(MsgTag, new FacesMessage("功能修改失败",	"不存在功能funcId=" + function.getId()));
			return;
		}
		// 保存
		sysFunctionManager.merge(function);
		sysLogManager.addLog(session.getUserName(), "功能修改成功!funcId=" + function.getId());
		context.addMessage(MsgTag, new FacesMessage("功能修改成功", "funcId=" + function.getId()));
		search();
	}

	public void delete(int funcId) {
		FacesContext context = FacesContext.getCurrentInstance();

		List<EntitySysRoleFunction> list = sysRoleFunctionManager.findBy(EntitySysRoleFunction.K.functionId,funcId);
		if(null != list && !list.isEmpty()){
			context.addMessage(MsgTag, new FacesMessage("功能删除失败", "功能已经分配给角色funcId="+ funcId));
			return;
		}

		if (null != sysFunctionManager.get(funcId)) {
			sysFunctionManager.removeById(funcId);
			sysLogManager.addLog(session.getUserName(), "功能删除成功!funcId=" + funcId);
			context.addMessage(MsgTag, new FacesMessage("功能删除成功", "功能funcId="	+ funcId));
			search();
		}
	}

	public EntitySysFunction getFunction() {
		return function;
	}

	public void setFunction(EntitySysFunction function) {
		this.function = function;
	}

	public Map<Integer, EntitySysModel> getModelMap() {
		return modelMap;
	}

	public void setModelMap(Map<Integer, EntitySysModel> modelMap) {
		this.modelMap = modelMap;
	}

	public Map<Integer, String> getStatusMap() {
		return statusMap;
	}

	public void setStatusMap(Map<Integer, String> statusMap) {
		this.statusMap = statusMap;
	}

	public List<EntitySysFunction> getFunctionList() {
		return functionList;
	}

	public void setFunctionList(List<EntitySysFunction> functionList) {
		this.functionList = functionList;
	}
}
